// 用于返回统一格式的接口数据格式

import { ResultEnum } from '@/apis/http/httpEnum'

export function resultSuccess<T = Recordable>(
  data: T,
  { msg = 'success' } = {}
) {
  return {
    code: ResultEnum.SUCCESS,
    data,
    msg
  }
}

export function resultPageSuccess<T = any>(
  offset: number,
  limit: number,
  dataSource: T[],
  { message = 'ok' } = {}
) {
  const list = dataSource.slice(offset, offset + limit)
  return {
    ...resultSuccess({
      list,
      meta: {
        offset,
        limit,
        total: dataSource.length
      }
    }),
    message
  }
}

export function resultError(
  msg = 'Request failed',
  { code = ResultEnum.ERROR, result = null } = {}
) {
  return {
    code,
    result,
    msg
  }
}

export interface requestParams {
  method: string
  body: any
  headers?: { authorization?: string }
  query: any
}

/**
 * @description 本函数用于从request数据中获取token，请根据项目的实际情况修改
 *
 */
export function getRequestToken({
  headers
}: requestParams): string | undefined {
  return headers?.authorization
}
